package com.ruoyi.workflow.service.impl;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.flowable.FlowableServiceUrl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.rest.RestTemplateUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.workflow.domain.HistoryTaskResponse;
import com.ruoyi.workflow.domain.TaskTodo;
import com.ruoyi.workflow.domain.TaskTodoResponse;
import com.ruoyi.workflow.service.ITaskTodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import java.util.Map;

/**
 * 待办任务Service业务层处理
 *
 * @author yuanpingfang
 * @date 2021-04-09
 */
@Service
public class TaskTodoServiceImpl extends RestTemplateUtil implements ITaskTodoService {
    @Autowired
    private FlowableServiceUrl flowableServiceUrl;

    /**
     * 查询待办任务
     *
     * @param taskId 待办任务ID
     * @return 待办任务
     */
    @Override
    public TaskTodo selectTaskTodoById(String taskId) {

        return null;
    }

    /**
     * 查询待办任务列表
     *
     * @param taskTodo 待办任务
     * @return 待办任务
     */
    @Override
    public TaskTodoResponse selectTaskTodoList(TaskTodo taskTodo, Integer pageNum, Integer pageSize) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", user.getUserId().toString());
        map.put("auditType", "");
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        Map<String, Object> returnMap = doPost(flowableServiceUrl.getQueryUnDoTaskList(), map);
        String paramsJson = JSONUtil.toJsonStr(returnMap);
        TaskTodoResponse taskTodoResponse = JSONUtil.toBean(paramsJson, TaskTodoResponse.class);
        return taskTodoResponse;
    }

    /**
     * 审核待办任务
     *
     * @param taskTodo 待办任务
     * @return 结果
     */
    @Override
    public AjaxResult completeTaskTodo(TaskTodo taskTodo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("taskId", taskTodo.getTaskId());
        map.put("taskStatus", taskTodo.getTaskStatus());
        map.put("taskMessage", "agree".equals(taskTodo.getTaskStatus()) ? "同意" : "不同意");
        map.put("taskComment", taskTodo.getTaskComment());
        Map<String, Object> approveUserMap = new HashMap<>();
        approveUserMap.put("approveUserId", user.getUserId().toString());
        map.put("approveUserMap", approveUserMap);
        return doPost(flowableServiceUrl.getCompleteTask(), map);
    }

    @Override
    public HistoryTaskResponse queryHistoryTask(String processInstanceId) {
        Map<String, Object> map = new HashMap<>();
        map.put("processInstanceId", processInstanceId);
        AjaxResult result = doPost(flowableServiceUrl.getQueryHistoryTask(), map);
        String paramsJson = JSONUtil.toJsonStr(result);
        HistoryTaskResponse historyTaskResponse = JSONUtil.toBean(paramsJson, HistoryTaskResponse.class);
        return historyTaskResponse;
    }

    @Override
    public AjaxResult terminateProcess(TaskTodo taskTodo) {
        Map<String, Object> map = new HashMap<>();
        map.put("processInstanceId", taskTodo.getProcessInstanceId());
        map.put("deleteReason", taskTodo.getDeleteReason());
        return doPost(flowableServiceUrl.getTerminateProcess(), map);
    }

}
